package com.qinchong.listview;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.qinchong.listview.bean.News;
/**
 * 简单的ListView分批加载
 * @author qinchong
 *
 */
public class PaginationListViewActivity extends Activity implements
		OnScrollListener {

	private ListView listView;
	private int visibleLastIndex = 0; // 最后的可视项索引
	private int visibleItemCount; // 当前窗口可见项总数
	private int datasize = 38; // 模拟数据集的条数
	private PaginationAdapter adapter;
	private View loadMoreView;
	private Button loadMoreButton;
	private Handler handler = new Handler();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		loadMoreView = getLayoutInflater().inflate(R.layout.loadmore, null);
		loadMoreButton = (Button) loadMoreView
				.findViewById(R.id.loadMoreButton);
		loadMoreButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				loadMoreButton.setText("正在加载中..."); // 设置按钮文字
				handler.postDelayed(new Runnable() {

					@Override
					public void run() {
						loadMoreData();
						adapter.notifyDataSetChanged();
						loadMoreButton.setText("查看更多..."); // 恢复按钮文字
					}
				}, 2000);

			}
		});

		listView = (ListView) findViewById(R.id.listView_main);
		listView.addFooterView(loadMoreView); // 设置列表底部视图
		initializeAdapter();
		listView.setAdapter(adapter);
		listView.setOnScrollListener(this);
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		int itemsLastIndex = adapter.getCount() - 1; // 数据集最后一项的索引
		int lastIndex = itemsLastIndex + 1;
		if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
				&& visibleLastIndex == lastIndex) {
			// 如果是自动加载,可以在这里放置异步加载数据的代码
		}
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		this.visibleItemCount = visibleItemCount;
		visibleLastIndex = firstVisibleItem + visibleItemCount - 1;

		Log.e("========================= ", "========================");
		Log.e("firstVisibleItem = ", firstVisibleItem + "");
		Log.e("visibleItemCount = ", visibleItemCount + "");
		Log.e("totalItemCount = ", totalItemCount + "");
		Log.e("========================= ", "========================");

		// 如果所有的记录选项等于数据集的条数，则移除列表底部视图
		if (totalItemCount == datasize + 1) {
			listView.removeFooterView(loadMoreView);
			Toast.makeText(this, "数据全部加载完!", Toast.LENGTH_LONG).show();
		}
	}

	/**
	 * 初始化ListView的适配器
	 */
	private void initializeAdapter() {
		List<News> news = new ArrayList<News>();
		for (int i = 1; i <= 10; i++) {
			News items = new News();
			items.setTitle("Title" + i);
			items.setContent("This is News Content" + i);
			news.add(items);
		}
		adapter = new PaginationAdapter(news);
	}

	/**
	 * 加载更多数据
	 */
	private void loadMoreData() {
		int count = adapter.getCount();

		if (count + 10 <= datasize) {
			for (int i = count + 1; i <= count + 10; i++) {
				News item = new News();
				item.setTitle("Title" + i);
				item.setContent("This is News Content" + i);
				adapter.addNewsItem(item);
			}
		} else {
			for (int i = count + 1; i <= datasize; i++) {
				News item = new News();
				item.setTitle("Title" + i);
				item.setContent("This is News Content" + i);
				adapter.addNewsItem(item);
			}
		}

	}

	class PaginationAdapter extends BaseAdapter {

		List<News> newsItems;

		public PaginationAdapter(List<News> newsitems) {
			this.newsItems = newsitems;
		}

		@Override
		public int getCount() {
			return newsItems.size();
		}

		@Override
		public Object getItem(int position) {
			return newsItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {
			if (view == null) {
				view = getLayoutInflater().inflate(R.layout.list_item, null);
			}

			// 新闻标题
			TextView tvTitle = (TextView) view.findViewById(R.id.newstitle);
			tvTitle.setText(newsItems.get(position).getTitle());
			// 新闻内容
			TextView tvContent = (TextView) view.findViewById(R.id.newscontent);
			tvContent.setText(newsItems.get(position).getContent());

			return view;
		}

		/**
		 * 添加数据列表项
		 * 
		 * @param newsitem
		 */
		public void addNewsItem(News newsitem) {
			newsItems.add(newsitem);
		}

	}

}
